package com.cjkj.system.service.impl;

import com.cjkj.common.core.exception.ServiceException;
import com.cjkj.common.core.utils.DateUtils;
import com.cjkj.common.core.utils.uuid.IdUtils;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.domain.THospitalDept;
import com.cjkj.system.api.domain.THospitalUser;
import com.cjkj.system.domain.THospitalEquipment;
import com.cjkj.system.domain.dto.HospitalDeptReq;
import com.cjkj.system.domain.vo.DeptTreeVo;
import com.cjkj.system.domain.vo.HospitalDeptVo;
import com.cjkj.system.mapper.THospitalDeptMapper;
import com.cjkj.system.mapper.THospitalEquipmentMapper;
import com.cjkj.system.mapper.THospitalUserMapper;
import com.cjkj.system.service.ITHospitalDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 医院科室信息Service业务层处理
 * 
 * @author cjkj
 * @date 2024-08-30
 */
@Service
public class THospitalDeptServiceImpl implements ITHospitalDeptService 
{
    @Resource
    private THospitalDeptMapper tHospitalDeptMapper;

    @Resource
    private THospitalUserMapper tHospitalUserMapper;

    @Resource
    private THospitalEquipmentMapper tHospitalEquipmentMapper;

    @Override
    public List<DeptTreeVo> selectDeptTreeList(String hospitalId) {
        return tHospitalDeptMapper.selectDeptTree(hospitalId);
    }

    /**
     * 查询医院科室信息
     * 
     * @param deptId 医院科室信息主键
     * @return 医院科室信息
     */
    @Override
    public THospitalDept selectTHospitalDeptByDeptId(String deptId)
    {
        return tHospitalDeptMapper.selectTHospitalDeptByDeptId(deptId);
    }

    /**
     * 查询医院科室信息列表
     * 
     * @param req 医院科室信息
     * @return 医院科室信息
     */
    @Override
    public List<HospitalDeptVo> selectTHospitalDeptList(HospitalDeptReq req)
    {
        return tHospitalDeptMapper.selectTHospitalDeptList(req);
    }

    /**
     * 新增医院科室信息
     * 
     * @param tHospitalDept 医院科室信息
     * @return 结果
     */
    @Override
    public int insertTHospitalDept(THospitalDept tHospitalDept)
    {
        String hospitalId = tHospitalDept.getHospitalId();
        String deptName = tHospitalDept.getDeptName();
        THospitalDept baseInfo = tHospitalDeptMapper.selectByHosIdAndName(hospitalId, deptName);
        if(baseInfo != null) {
            throw new ServiceException("已存在名为" + deptName + "的科室！");
        }

        tHospitalDept.setDeptId(IdUtils.fastSimpleUUID());
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        tHospitalDept.setCreateBy(username);
        tHospitalDept.setCreateTime(nowDate);
        tHospitalDept.setUpdateBy(username);
        tHospitalDept.setUpdateTime(nowDate);
        return tHospitalDeptMapper.insertTHospitalDept(tHospitalDept);
    }

    /**
     * 修改医院科室信息
     * 
     * @param tHospitalDept 医院科室信息
     * @return 结果
     */
    @Override
    public int updateTHospitalDept(THospitalDept tHospitalDept)
    {
        String hospitalId = tHospitalDept.getHospitalId();
        String deptName = tHospitalDept.getDeptName();
        THospitalDept baseInfo = tHospitalDeptMapper.selectByHosIdAndName(hospitalId, deptName);

        if(baseInfo != null && ! baseInfo.getDeptId().equals(tHospitalDept.getDeptId())) {
            throw new ServiceException("已存在名为" + deptName + "的科室！");
        }

        tHospitalDept.setUpdateBy(SecurityUtils.getUsername());
        tHospitalDept.setUpdateTime(DateUtils.getNowDate());
        return tHospitalDeptMapper.updateTHospitalDept(tHospitalDept);
    }

    /**
     * 删除医院科室信息信息
     * 
     * @param deptId 医院科室信息主键
     * @return 结果
     */
    @Override
    public int deleteTHospitalDeptByDeptId(String deptId)
    {
        // 1、查询科室信息
        THospitalDept tHospitalDept = tHospitalDeptMapper.selectTHospitalDeptByDeptId(deptId);
        if(tHospitalDept == null) {
            throw new ServiceException("科室信息不存在！");
        }
        String deptName = tHospitalDept.getDeptName();

        // 2、查询是否科室下的账号
        List<THospitalUser> tHospitalUsers = tHospitalUserMapper.selectUsersByDeptId(deptId);
        if(!tHospitalUsers.isEmpty()) {
            throw new ServiceException("科室" + deptName + "有正在使用的账号，不能删除！");
        }

        // 3、判断是否有分配到该科室的设备
        List<THospitalEquipment> equipments = tHospitalEquipmentMapper.selectByDept(deptId);
        if(!equipments.isEmpty()) {
            throw new ServiceException("科室" + deptName + "已分配设备，不能删除！");
        }

        // 4、删除科室
        return tHospitalDeptMapper.deleteTHospitalDeptByDeptId(deptId);
    }
}
